#include "stdafx.h"
#include "utils.h"
#include "scoped_array.h"
#include "CMFT.h"
#include "Attrs.h"
#include <QDateTime>

// QTextStream os(stdout);
// QTextStream is(stdin);

void SafeCloseHandle( HANDLE &hVol ) 
{
	if(hVol!=INVALID_HANDLE_VALUE) {
		CloseHandle(hVol);
		hVol = INVALID_HANDLE_VALUE;
	}
}

QString wArrayToQString( WCHAR* ptr, int len ) 
{
	return QString::fromWCharArray(ptr,len);
}


QString ToString(   const SYSTEMTIME*sysTime ) 
{
	return QString::number(sysTime->wYear)+"/"+QString::number(sysTime->wMonth).rightJustified(2,'0')+
		"/"+QString::number(sysTime->wDay).rightJustified(2,'0')+"_"+QString::number(sysTime->wHour).rightJustified(2,'0')+
		":"+QString::number(sysTime->wMinute).rightJustified(2,'0')+":"+QString::number(sysTime->wSecond).rightJustified(2,'0')+
		'.'+
		QString::number(sysTime->wMilliseconds).rightJustified(3,'0');
}

void printMFTInfo( CMFT* mft ) 
{
	QTextStream os(stdout);
	os<<"MFT:\""<<mft->getFileName()<<"\" "<<"EntryNo: "<<mft->entryNo()<<endl;
	NTFSFileTime times = mft->getTimes();
	os<<"FN Times:"<<endl;
	os<<"\tA:"<<ToString(&times.fileNameTime.access)<<endl;
	os<<"\tMFTM:"<<ToString(&times.fileNameTime.entry)<<endl;
	os<<"\tC:"<<ToString(&times.fileNameTime.creation)<<endl;
	os<<"\tM:"<<ToString(&times.fileNameTime.modify)<<endl;
	os<<"StdInfo Times:"<<endl;
	os<<"\tA:"<<ToString(&times.stdInfoTime.access)<<endl;
	os<<"\tMFTM:"<<ToString(&times.stdInfoTime.entry)<<endl;
	os<<"\tC:"<<ToString(&times.stdInfoTime.creation)<<endl;
	os<<"\tM:"<<ToString(&times.stdInfoTime.modify)<<endl;
	os<<"Resident : \n";
	for (int i = 0;i<mft->resAttrs.size();i++) {
		int typeId = mft->resAttrs[i]->Header.TypeID;
		int id = mft->resAttrs[i]->Header.ID;
		CAttribute att = mft->getAttribute(typeId,id);
		os<<"\tTYPE: "<<typeId<<" ,ID: "<<id<<" ,Name: "<<att.name()<<endl;
	}
	os<<"Non-Resident : \n";
	for (int i = 0;i<mft->nresAttrs.size();i++) {
		int typeId = mft->nresAttrs[i]->Header.TypeID;
		int id = mft->nresAttrs[i]->Header.ID;
		CAttribute att = mft->getAttribute(typeId,id);
		os<<"\tTYPE: "<<typeId<<" ,ID: "<<id<<" ,Name: "<<att.name()<<endl;
		QList<Range> cr = att.getClusterRange();
		for (int i = 0;i<cr.size();i++)
			os<<cr[i].start<<"-"<<cr[i].end<<" ";
		os<<endl;
	}
}

SYSTEMTIME ToSYSTEMTIME( QDateTime dt ) {
	SYSTEMTIME ret;
	QDate date = dt.date();
	QTime time = dt.time();
	ret.wDay = date.day();
	ret.wDayOfWeek = date.dayOfWeek();
	ret.wHour = time.hour();
	ret.wMilliseconds = time.msec();
	ret.wMinute = time.minute();
	ret.wMonth = date.month();
	ret.wSecond = time.second();
	ret.wYear = date.year();
	return ret;
}

QString ToString(QDateTime dt) {
	return ToString(&ToSYSTEMTIME(dt));
}

QDateTime toDateTime(QString str) {
	str = str.trimmed();
	int yy,mm,dd,hh,minute,sec=0,misec=0;
	int timezoneoffset = 0;
	int p=0;

	if (str.endsWith("(UTC)")) {
		timezoneoffset = 8;
		str.chop(5);
	}
	if (sscanf_s(str.toStdString().c_str(),"%d-%d-%d  %d:%d:%d",&yy,&mm,&dd,&hh,&minute,&sec )!=6)
		sscanf_s(str.toStdString().c_str(),"%d/%d/%d_%d:%d:%d.%d",&yy,&mm,&dd,&hh,&minute,&sec,&misec);
	return QDateTime(QDate(yy,mm,dd),QTime(hh,minute,sec,misec)).addSecs(timezoneoffset*60*60);
}